package com.thunbu.lib_common.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.thunbu.lib_common.config.CommConstants;

import java.io.Serializable;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;


public class MFSPHelper {
    private static final String TAG = "MFSPHelper";

    private static SharedPreferences sp;

    //在Application中初始化
    public static void initialize(Context context) {
        sp = context.getSharedPreferences(CommConstants.MEETING_SYSTEM,
                Context.MODE_PRIVATE);
    }

    public static String getString(String key) {
        return sp.getString(key, "");
    }

    /**
     * @param key key
     * @param def 默认值
     * @return value
     */
    public static String getString(String key, String def) {
        return sp.getString(key, def);
    }

    public static void setString(String key, String value) {
        Editor editor = sp.edit();
        editor.putString(key, value);
        boolean commit = editor.commit();
    }

    public static int getInteger(String key, int defValue) {
        return sp.getInt(key, defValue);
    }

    public static int getInteger(String key) {
        return sp.getInt(key, -1);
    }

    public static int getSize(String key) {
        return sp.getInt(key, 0);
    }

    public static void setSize(String key, int value) {
        Editor editor = sp.edit();
        editor.putInt(key, value);
        editor.commit();
    }

    public static int getCount(String key) {
        return sp.getInt(key, 0);
    }

    public static void setInteger(String key, int value) {
        Editor editor = sp.edit();
        editor.putInt(key, value);
        editor.commit();
    }

    public static boolean getBoolean(String key, boolean bool) {
        return sp.getBoolean(key, bool);
    }

    public static void setBoolean(String key, Boolean value) {
        Editor editor = sp.edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    public static Set<String> getStringSet(String key) {
        return sp.getStringSet(key, new HashSet<String>());

    }

    public static void setStringSet(String key, Set<String> value) {
        Editor editor = sp.edit();
        editor.putStringSet(key, value);
        editor.commit();
    }

    public static <T> List<T> getObjectList(String key, Class<T> clazz) {
        List<T> objectList = new ArrayList<>();
        String strJson = sp.getString(key, null);
        if (TextUtils.isEmpty(strJson)) {
            return objectList;
        }
        Gson gson = new Gson();
//        try {
//            Type type = TypeBuilder
//                    .newInstance(List.class)
//                    .addTypeParam(clazz)
//                    .build();
//            List<T> list = gson.fromJson(strJson, type);
//            if (list != null && !list.isEmpty()) {
//                objectList.addAll(list);
//            }
//        } catch (Exception e) {
//            //XLog.e(TAG, e);
//        }
        return objectList;
    }
    public static List<String> getStringList(String key) {
        List<String> list = new ArrayList<>();
        String json = sp.getString(key, "");
        if (!TextUtils.isEmpty(json) && json.length() > 0) {
            Gson gson = new Gson();
            JsonArray array = new JsonParser().parse(json).getAsJsonArray();
            for (JsonElement elem : array) {
                list.add(gson.fromJson(elem, String.class));
            }
        }
        return list;
    }

    public static void setStringList(String key, List<String> value) {
        Editor editor = sp.edit();
        JsonArray array = new JsonArray();
        for (String string : value) {
            array.add(string);
        }
        editor.putString(key, array.toString());
        editor.commit();
        editor.apply();
    }

    public static <T> void setObjectList(String key, List<T> datalist) {
        if (null == datalist || datalist.size() <= 0) {
            return;
        }
        Editor editor = sp.edit();
        Gson gson = new Gson();
        //转换成json数据，再保存
        String strJson = gson.toJson(datalist);
        editor.putString(key, strJson);
        editor.commit();
    }

    public static <K, V> void setObjectMap(String key, Map<K, V> map) {
        if (null == map) {
            return;
        }
        Editor editor = sp.edit();
        Gson gson = new Gson();
        //转换成json数据，再保存
        String strJson = gson.toJson(map);
        editor.putString(key, strJson);
        editor.commit();
    }


    public static long getLong(String key) {
        return sp.getLong(key, -1);
    }

    public static long getTime(String key) {
        return sp.getLong(key, 28800000);
    }

    public static void setLong(String key, long value) {
        Editor editor = sp.edit();
        editor.putLong(key, value);
        editor.commit();
    }

    //------------------------------------- 保存对象 ------------------------------------//
    private static <E> Editor put(@NonNull String key, @NonNull E value) {
        Editor editor = sp.edit();
        if (value instanceof String) {
            editor.putString(key, (String) value);
        } else if (value instanceof Integer
                || value instanceof Boolean
                || value instanceof Float
                || value instanceof Long
                || value instanceof Double) {
            editor.putString(key, String.valueOf(value));
        } else {
            editor.putString(key, new Gson().toJson(value));
        }
        return editor;
    }

    public static <E> E get(@NonNull String key, @NonNull E defaultValue) {
        String value = sp.getString(key, JsonUtils.toJson(defaultValue));
        if (defaultValue instanceof String) {
            return (E) value;
        }
        if (defaultValue instanceof Integer) {
            return (E) Integer.valueOf(value);
        }
        if (defaultValue instanceof Boolean) {
            return (E) Boolean.valueOf(value);
        }
        if (defaultValue instanceof Float) {
            return (E) Float.valueOf(value);
        }
        if (defaultValue instanceof Long) {
            return (E) Long.valueOf(value);
        }
        if (defaultValue instanceof Double) {
            return (E) Double.valueOf(value);
        }
        //json为null的时候返回对象为null,gson已处理
        return (E) new Gson().fromJson(value, defaultValue.getClass());
    }

    public static <E extends Serializable> void saveObject(String key, E value) {
        put(key, value).commit();
    }

    public static <E> E readObject(String key, Class<E> clazz) {
        try {
            return get(key, clazz.newInstance());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    //------------------------------------- 保存对象 ------------------------------------//

    public static void clear() {
        Editor editor = sp.edit();
        editor.clear();
        editor.commit();
    }

    public static void remove(String key) {
        Editor editor = sp.edit();
        editor.remove(key);
        editor.commit();
    }

    public static boolean hasKey(String key){
        return sp.contains(key);
    }

}